package com.briup.bookstore.web.controller;

import com.briup.bookstore.dto.AddressAddDTO;
import com.briup.bookstore.dto.AddressUpdateDTO;
import com.briup.bookstore.response.Result;
import com.briup.bookstore.service.AddressService;
import com.briup.bookstore.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/7/26 下午3:13
 * @Description
 **/
@RestController
@RequestMapping("address")
@Api(tags = "收货地址模块")
public class AddressController {
    @Autowired
    private AddressService addressService;



    @GetMapping
    @ApiOperation("获取用户的所有收货地址")
    public Result getAddresses(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        List<AddressVO> list = addressService.findAddresses(Integer.parseInt(userId));
        return  Result.success(list);

    }
    @PostMapping
    @ApiOperation("新增收货地址")
    public Result add(AddressAddDTO addressAddDTO){
        addressService.save(addressAddDTO);
        return  Result.success();
    }
    @PutMapping
    @ApiOperation("修改收货信息")
    public Result update(@RequestBody AddressUpdateDTO addressUpdateDTO){
        addressService.update(addressUpdateDTO);
        return  Result.success();
    }
    @PutMapping("setDefault/{addressId}")
    @ApiOperation("设置用户默认的收货地址")
    public Result setDefaultAddress(HttpServletRequest request,@PathVariable("addressId") Integer id){
        String userId = (String) request.getAttribute("userId");
        addressService.setDefaultAddress(Integer.parseInt(userId),id);
        return  Result.success();
    }
    @PutMapping("/cancelDefault/{addressId}")
    @ApiOperation("取消用户默认的收货地址")
    public Result cancelDefaultAddress(@PathVariable("addressId") Integer id){
        addressService.cancelDefaultAddress(id);
        return  Result.success();
    }
    @DeleteMapping
    @ApiOperation("删除收货地址")
    public Result delete(Integer id){

        addressService.delete(id);
        return  Result.success();
    }
}
